package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.ResultConstants;
import com.ruoyi.common.enums.AmountType;
import com.ruoyi.common.enums.TxnType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.system.domain.WdyTxnHistory;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.domain.WdyVcFundAssetDailyProfit;
import com.ruoyi.system.mapper.WdyVcFundAssetDailyProfitMapper;
import com.ruoyi.system.service.IWdyTxnHistoryService;
import com.ruoyi.system.service.IWdyUserService;
import com.ruoyi.system.service.IWdyVcFundAssetDailyProfitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * vc基金资产日收益 服务层实现
 * 
 * @author ruoyi
 * @date 2020-04-04
 */
@Service
public class WdyVcFundAssetDailyProfitServiceImpl extends ServiceImpl<WdyVcFundAssetDailyProfitMapper, WdyVcFundAssetDailyProfit> implements IWdyVcFundAssetDailyProfitService
{

	@Autowired
	private IWdyUserService wdyUserService;

	@Autowired
	private IWdyTxnHistoryService wdyTxnHistoryService;
	/**
     * 查询vc基金资产日收益信息
     * 
     * @param id vc基金资产日收益ID
     * @return vc基金资产日收益信息
     */
    @Override
	public WdyVcFundAssetDailyProfit selectWdyVcFundAssetDailyProfitById(Long id)
	{
	    return baseMapper.selectWdyVcFundAssetDailyProfitById(id);
	}
	
	/**
     * 查询vc基金资产日收益列表
     * 
     * @param wdyVcFundAssetDailyProfit vc基金资产日收益信息
     * @return vc基金资产日收益集合
     */
	@Override
	public List<WdyVcFundAssetDailyProfit> selectWdyVcFundAssetDailyProfitList(WdyVcFundAssetDailyProfit wdyVcFundAssetDailyProfit)
	{
	    return baseMapper.selectWdyVcFundAssetDailyProfitList(wdyVcFundAssetDailyProfit);
	}
	
    /**
     * 新增vc基金资产日收益
     * 
     * @param wdyVcFundAssetDailyProfit vc基金资产日收益信息
     * @return 结果
     */
	@Override
	public int insertWdyVcFundAssetDailyProfit(WdyVcFundAssetDailyProfit wdyVcFundAssetDailyProfit)
	{
	    return baseMapper.insertWdyVcFundAssetDailyProfit(wdyVcFundAssetDailyProfit);
	}
	
	/**
     * 修改vc基金资产日收益
     * 
     * @param wdyVcFundAssetDailyProfit vc基金资产日收益信息
     * @return 结果
     */
	@Override
	public int updateWdyVcFundAssetDailyProfit(WdyVcFundAssetDailyProfit wdyVcFundAssetDailyProfit)
	{
	    return baseMapper.updateWdyVcFundAssetDailyProfit(wdyVcFundAssetDailyProfit);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void grantDailyProfit(WdyVcFundAssetDailyProfit wdyVcFundAssetDailyProfit) {
		QueryWrapper<WdyUser> userQuery = new QueryWrapper<>();
		userQuery.eq("user_id", wdyVcFundAssetDailyProfit.getUserId());
		WdyUser user = wdyUserService.getOne(userQuery);

		WdyUser updateUser = new WdyUser();
		updateUser.setCnnWithdrawAmount(BigDecimalUtil.add(user.getCnnWithdrawAmount(), wdyVcFundAssetDailyProfit.getProfit()));
		updateUser.setVersion(user.getVersion());
		if(!wdyUserService.update(updateUser, userQuery)) {
			throw new BusinessException(ResultConstants.EX9110, "系统繁忙，请稍后重试");
		}

		this.save(wdyVcFundAssetDailyProfit);

		WdyTxnHistory txnHistory = new WdyTxnHistory();
		txnHistory.setUserId(wdyVcFundAssetDailyProfit.getUserId());
		txnHistory.setTxnId(String.valueOf(wdyVcFundAssetDailyProfit.getId()));
		txnHistory.setTxnAmount(wdyVcFundAssetDailyProfit.getProfit());
		txnHistory.setTxnType(TxnType.VC_FUND_DAILY_PROFIT.getCode());
		txnHistory.setAmountType(AmountType.CNN_WITHDRAW.getCode());
		txnHistory.setTxnDesc(TxnType.VC_FUND_DAILY_PROFIT.getName());
		wdyTxnHistoryService.save(txnHistory);

	}

	/**
     * 删除vc基金资产日收益对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteWdyVcFundAssetDailyProfitByIds(String ids)
	{
		return baseMapper.deleteWdyVcFundAssetDailyProfitByIds(Convert.toStrArray(ids));
	}
	
}
